﻿using AutoMapper;
using MediatR;
using Microsoft.IdentityModel.Abstractions;
using T.S.BaseData.Api.AppClinet.Commend.SlaugHter;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.SlaugHter
{
    public class SlaugHteringentAddCommandHandle : IRequestHandler<SlaugHteringentAddCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<SlaughteringEnterprises> slauEntRepo;
        private readonly IMapper mapper;
        private readonly ILogger<SlaugHteringentAddCommandHandle> logger;

        public SlaugHteringentAddCommandHandle(IBaseRepository<SlaughteringEnterprises> slauEntRepo,    
            IMapper mapper, 
            ILogger<SlaugHteringentAddCommandHandle> logger)
        {
            this.slauEntRepo = slauEntRepo;
            this.mapper = mapper;
            this.logger = logger;
        }

        public Task<ApiResult<int>> Handle(SlaugHteringentAddCommand request, CancellationToken cancellationToken)
        {

            ApiResult<int> result = new ApiResult<int>();

            var model = mapper.Map<SlaughteringEnterprises>(request);

            var r = slauEntRepo.Crate(model);
            if (r>0)
            {
                result.Code = ApiEnums.Success;
                result.Data = r;
                result.Meg = "添加成功";

                logger.LogInformation("添加成功");
            }
            else
            {
                result.Code = ApiEnums.Error;
                result.Data = r;
                result.Meg = "添加失败";

                logger.LogInformation("添加失败");
            }

            return Task.FromResult(result);
        }
    }
}
